package tools;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HTMLParser
{
	private ArrayList <ArrayList<String>> rows;
	
	public HTMLParser(String oracle_html)
	{
		rows = new ArrayList<ArrayList<String>>();
		
		int cell_no = 0;
		ArrayList<String> row = new ArrayList<String>();
		
		Elements cells = getCells(oracle_html);
		for (Element cell : cells)
		{
			row.add( cell.text() );
			cell_no++;
			
			if ( (cell_no != 0) && ((cell_no % 10) == 0) )
			{
				rows.add(row);
				row = new ArrayList<String>();
			}
			
		}
		
	}

	
	private Elements getCells(String file_path)
	{
		File file = new File(file_path);
		Document doc = null;
		Elements elements = null;
		
		try	
		{
			doc = Jsoup.parse(file, "UTF-8");
			elements = doc.getElementsByTag("td");
		}
		catch (Exception e) { throw new RuntimeException("Error parsing!"); }
		
		return elements;
	}

	public Collection<String> getRow (int row)
	{
		return rows.get(row);
	}
	
	public String getCell(int row, int col)
	{
		return rows.get(row).get(col);
	}


	public int getRowCount()
	{
		return rows.size();
	}

	
	
	
}
